package com.example.bangbangxia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bangbangxia.dao.TaskMapper;
import com.example.bangbangxia.domain.Task;
import com.example.bangbangxia.service.TaskService;
import com.example.bangbangxia.service.UserService;
import com.example.bangbangxia.utils.RespBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 任务的业务层实现类
 */
@Service
public class TaskServiceImpl implements TaskService {
    private Log log = LogFactory.getLog(TaskServiceImpl.class);

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private UserService userService;

    /**
     * 添加任务
     * @param task
     * @return
     */
    @Override
    public RespBean insertTask(Task task) {
        log.info("添加任务");
        if (taskMapper.insert(task)==1){
            return RespBean.ok();
        }
        return RespBean.error("添加任务失败");
    }

    /**
     * 删除任务
     * @param
     * @return
     */
    @Override
    public RespBean deleteTask(Integer taskId) {
        log.info("根据id删除任务:"+taskId);
        taskMapper.deleteById(taskId);
        return RespBean.ok();
    }

    /**
     * 更新任务
     * @param task
     * @return
     */
    @Override
    public RespBean updateTask(Task task) {
        log.info("更新任务");
        if (taskMapper.updateById(task)==1){
            return  RespBean.ok();
        }
        return RespBean.error("更新任务失败");
    }

    /**
     * 查询所有任务
     * @return
     */
    @Override
    public RespBean queryTaskList(Integer currentPage, Integer pageSize,Integer userId) {
        log.info("根据id查询所有任务:"+userId);
        IPage<Task> page = new Page<>(currentPage, pageSize);//分页构造器
        QueryWrapper<Task> wrapper = new QueryWrapper<>();//条件构造器
        wrapper.ne("user_id", userId) //id不是自己的
                .eq("task_state", 1) //任务状态为1：未被接
                .orderByDesc("create_time"); //按创建时间倒序排序
        taskMapper.selectPage(page, wrapper);//执行查询
        return RespBean.ok(page);
    }

    /**
     * 查看自己发布的任务（1.未被接/2.正在进行中/3.已完成)* / 查看自己接受的任务（2.正在进行/3.已完成）
     * @return
     */
    @Override
    public RespBean queryMyTask(Integer currentPage, Integer pageSize, Integer userId, Integer acceptUserId, Integer taskState) {
        log.info("根据id查询任务:"+userId);

        Page<Task> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Task> wrapper = new QueryWrapper<>();//条件构造器
        wrapper.orderByDesc("create_time")
                .eq(userId!=null && userId!=0, "user_id", userId) //如果userId不为空，则添加该条件
                .eq(acceptUserId!=null && acceptUserId!=0, "accept_userId", acceptUserId)
                .eq(taskState!=null, "task_state", taskState);
        taskMapper.selectPage(page, wrapper);
        return RespBean.ok(page);
    }


    public RespBean acceptTask(Task task, Integer myId) {
        //给任务接受人属性赋值
        task.setAcceptUserId(myId);
        //改变任务状态
        task.setTaskState(2);
        System.out.println("当前task:"+task.toString());
        //更新任务
        if (taskMapper.updateById(task)==1){
            return RespBean.ok();
        }else {
            return RespBean.error("抢任务失败");
        }
    }

}
